package com.zllkj.mm.base;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.graphics.Color;
import android.view.View;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zllkj.mm.R;
import com.zllkj.mm.common.L;
import com.zllkj.mm.widgets.CommonTopBar;


public abstract class BaseTitleFragment extends BaseAppFragment {

    public CommonTopBar mTitlebar;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_base_title;
    }

    @Override
    protected void onBindViewBefore(View rootView) {
        super.onBindViewBefore(rootView);
        ViewStub stub = (ViewStub) rootView.findViewById(R.id.layout_content);
        stub.setLayoutResource(getContentLayoutId());
        stub.inflate();
    }

    protected abstract
    @LayoutRes
    int getContentLayoutId();

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
       mTitlebar = (CommonTopBar) rootView.findViewById(R.id.top_bar);
    }


    public BaseTitleFragment setTitleText(String title) {
        if (mTitlebar != null) {
            mTitlebar.setTextCenter(title);
        }

        return this;
    }

    public void showEmpty(String title , String content){
        AppCompatTextView emptyTitle = mRootView.findViewById(R.id.tv_empty_title);
        AppCompatTextView emptyContent = mRootView.findViewById(R.id.tv_empty_content);
        AppCompatImageView emptyImage = mRootView.findViewById(R.id.iv_empty_icon);
        ConstraintLayout emptyLayout = mRootView.findViewById(R.id.rl_empty_display);

        if (emptyLayout != null && emptyTitle != null && emptyImage != null && emptyContent != null){
            emptyTitle.setText(title);
            emptyContent.setText(content);
        }
    }

    public void hideEmpty(){
        AppCompatTextView emptyTitle = mRootView.findViewById(R.id.tv_empty_title);
        AppCompatTextView emptyContent = mRootView.findViewById(R.id.tv_empty_content);
        AppCompatImageView emptyImage = mRootView.findViewById(R.id.iv_empty_icon);
        ConstraintLayout emptyLayout = mRootView.findViewById(R.id.rl_empty_display);
        if (emptyLayout != null && emptyTitle != null && emptyImage != null && emptyContent != null){
            emptyLayout.setVisibility(View.VISIBLE);
        }
    }
    public BaseTitleFragment setCommonTitle(String titleStr) {
        return setCommonTitle(titleStr, R.color.color_background);
    }


    public BaseTitleFragment setCommonTitle(@StringRes int titleId) {
        return setCommonTitle(getResources().getString(titleId), R.color.color_background);
    }

    public BaseTitleFragment setCommonTitle(@StringRes int titleId, @ColorRes int titleColorId) {
        return setCommonTitle(getResources().getString(titleId), titleColorId);
    }

    public BaseTitleFragment setCommonTitle(String titleStr, @ColorRes int titleColorId) {
        View rigthLayout = mTitlebar.findViewById(R.id.fl_title_right);
        rigthLayout.setVisibility(View.INVISIBLE);

        return setTitle(titleStr, R.color.black)
                .setTitleBgColor(titleColorId)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
    }

    public BaseTitleFragment setTitle(@StringRes int titleId, @ColorRes int titleColorId) {
        return setTitle(getResources().getString(titleId), titleColorId);
    }


    public BaseTitleFragment setTitle(String titleText, @ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleField = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleField.setText(titleText);
            titleField.setTextColor(getResources().getColor(titleColorId));
            mTitlebar.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public BaseTitleFragment withoutReturn() {
        if (mTitlebar != null) {
            mTitlebar.setShowImgLeft(false);
        }
        return this;
    }


    public BaseTitleFragment setTitleText(@StringRes int titleId) {
        if (mTitlebar != null) {
            mTitlebar.setTextCenter(getResources().getString(titleId));
        }
        return this;
    }

    public BaseTitleFragment setTitleColor(@ColorRes int titleColorId) {
        if (mTitlebar != null) {
            TextView titleText = (TextView) mTitlebar.findViewById(R.id.tv_title_center);
            titleText.setTextColor(getResources().getColor(titleColorId));
        }
        return this;
    }

    public BaseTitleFragment setTitleBgColor(@ColorRes int bgColorId) {
        if (mTitlebar != null) {
            ImageView bgView = (ImageView) mTitlebar.findViewById(R.id.iv_title_bg);
            if (bgView != null) {
                if (bgColorId == 0) {
                    bgView.setBackgroundColor(getResources().getColor(R.color.color_background));
                } else {
                    bgView.setBackgroundColor(getResources().getColor(bgColorId));
                }
            }
//            mTitlebar.setBackgroundColor(getResources().getColor(bgColorId));
        }

        return this;
    }


    public BaseTitleFragment setImgLeftBg(@DrawableRes int id) {
        if (mTitlebar != null) {
            mTitlebar.setImgLeft(id);
            mTitlebar.setShowImgLeft(true);
        }

        return this;
    }

    public TextView rightText;

    public BaseTitleFragment setRightBtn(String rightTextStr, View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            View rigthLayout = mTitlebar.findViewById(R.id.fl_title_right);
            rigthLayout.setVisibility(View.VISIBLE);
            rightText = (TextView) mTitlebar.findViewById(R.id.tv_title_right);
            rightText.setTextColor(Color.BLACK);
            rightText.setVisibility(View.VISIBLE);
            rightText.setOnClickListener(clickListener);
            rightText.setText(rightTextStr);
        }
        return this;
    }

    public BaseTitleFragment setRightImg(int rightSrc, View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            View rigthLayout = mTitlebar.findViewById(R.id.fl_title_right);
            rigthLayout.setVisibility(View.VISIBLE);
            rigthLayout.findViewById(R.id.iv_title_right).setOnClickListener(clickListener);
            ImageView imageView = (ImageView) mTitlebar.findViewById(R.id.iv_title_right);
            imageView.setVisibility(View.VISIBLE);
            L.setImg(rightSrc, imageView);
        }
        return this;
    }

    public BaseTitleFragment setRightImg2(int rightSrc, View.OnClickListener clickListener) {
        if (mTitlebar != null) {
            mTitlebar.setShowRight(true);
            View rigthLayout = mTitlebar.findViewById(R.id.fl_title_right);
            rigthLayout.setVisibility(View.VISIBLE);
            rigthLayout.findViewById(R.id.iv_title_right2).setOnClickListener(clickListener);
            ImageView imageView = (ImageView) mTitlebar.findViewById(R.id.iv_title_right2);
            imageView.setVisibility(View.VISIBLE);
            L.setImg(rightSrc, imageView);
        }
        return this;
    }


    /**
     * 设置左边图片 监听器
     */
    public BaseTitleFragment setImgLeftListener(View.OnClickListener listener) {
        if (mTitlebar != null) {
            FrameLayout leftLayout = (FrameLayout) mTitlebar.findViewById(R.id.fl_title_left);
            leftLayout.setOnClickListener(listener);
        }

        return this;
    }

    public BaseTitleFragment hideTitle() {
        if (mTitlebar != null) {
            mTitlebar.setVisibility(View.GONE);
        }
        return this;
    }

    public BaseTitleFragment hideDivider() {
        if (mTitlebar != null) {
//            View divider = mTitlebar.findViewById(R.id.title_divider);
//            divider.setVisibility(View.GONE);
        }
        return this;
    }

    public BaseTitleFragment setShowImgLeft(boolean showImgLeft) {
        if (mTitlebar != null) {
            mTitlebar.setShowImgLeft(showImgLeft);
        }
        return this;
    }


}
